<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace plugin\friend_link\user\controller;
use temmoku\controller;
use temmoku\db;
class friend extends controller{
	
	public function index(){
		$data=db::select('*')->from(P_jab."friend")->where("uid=".C('MYDB.uid'))->orderByDESC(['reorder'])->query();
		$this->assign('data', $data);
		if(DEVICE != 'pc'){
			$this->assign('webname','我的申请');
		}
		$this->display(tpl('index'));
	}
	public function add(){
		
		if('post'===$_POST['step']){
			if(!$_POST['title']){
				$this->err('链接名称不可以为空');
			}
			if(!$_POST['link']){
				$this->err('链接地址不可以为空');
			}
			if(C('FRIEND_LINK.is_yzm')){
				$yzm=\temmoku\lib\yzm::get_yzm_code($_POST['yzm']);
				if($yzm['code'] !='0'){
					$this->err('验证码错误');
				}
			}
			if($_POST['cid']){
				$data=db::select('*')->from(P_jab."category")->where('cid='.$_POST['cid'])->row();
				$name=$data['name'];
			}else{
				$name='默认分类';
			}
			if(C('FRIEND_LINK.is_w')==0){
				if($_POST['thumb'] && !preg_match('/(http|https):\/\/([\w\d\-_]+[\.\w\d\-_]+)[:\d+]?([\/]?[\w\/\.]+)/', $_POST['thumb'] )){
					$_POST['thumb']=UploadFiles_MOVE($_POST['thumb'],'Friendlink');
				}
			}
			$arr=[
				'title'=>$_POST['title'],
				'link'=>$_POST['link'],
				'thumb'=>$_POST['thumb'],
				'cid'=>$_POST['cid'],
				'state'=>0,
				'name'=>$name,
				'uid'=>C('mydb.uid')
			];
			db::insert(P_jab."friend")->cols($arr)->query();
			$this->Success('添加成功');
		}
		if(DEVICE != 'pc'){
			$this->assign('webname','申请友情链接');
		}
		$cdata=db::select('*')->from(P_jab."category")->orderByDESC(['reorder'])->query();
		$this->assign('cdata', $cdata);
		$this->display(tpl('add'));
	}
	
	public function edit(){
		if('post'===$_POST['step']){
			$data=db::select('*')->from(P_jab."friend")->where('id='.$_POST['id']." AND uid=".C('mydb.uid'))->row();
			if(!$data){
				$this->err('不存在的数据或无权修改');
			}
			if(!$_POST['id']){
				$this->err('缺少必要参数');
			}
			if(!$_POST['title']){
				$this->err('链接名称不可以为空');
			}
			if(!$_POST['link']){
				$this->err('链接地址不可以为空');
			}
			if(C('FRIEND_LINK.is_yzm')){
				$yzm=\temmoku\lib\yzm::get_yzm_code($_POST['yzm']);
				if($yzm['code'] !='0'){
					$this->err('验证码错误');
				}
			}
			if($_POST['cid']){
				$data=db::select('*')->from(P_jab."category")->where('cid='.$_POST['cid'])->row();
				$name=$data['name'];
			}else{
				$name='默认分类';
			}
			
			if(C('FRIEND_LINK.is_w')==0){
				if($_POST['thumb'] && !preg_match('/(http|https):\/\/([\w\d\-_]+[\.\w\d\-_]+)[:\d+]?([\/]?[\w\/\.]+)/', $_POST['thumb'] )){
					if($data['thumb'] != $_POST['thumb']){
						if($data['thumb']&& !preg_match('/(http|https):\/\/([\w\d\-_]+[\.\w\d\-_]+)[:\d+]?([\/]?[\w\/\.]+)/', $data['thumb'] ) ){
							$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$data['thumb'];
		    				//删除备份文件
							del_article_images($oldthumb);
		    				@unlink($oldthumb);
						}
						$_POST['thumb']=UploadFiles_MOVE($_POST['thumb'],'Friendlink');
					}
				}else{
					if($data['thumb']&& !preg_match('/(http|https):\/\/([\w\d\-_]+[\.\w\d\-_]+)[:\d+]?([\/]?[\w\/\.]+)/', $data['thumb'] ) ){
						$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$data['thumb'];
		    			//删除备份文件
						del_article_images($oldthumb);
		    			@unlink($oldthumb);
					}
				}
			}
			$arr=[
				'title'=>$_POST['title'],
				'link'=>$_POST['link'],
				'thumb'=>$_POST['thumb'],
				'cid'=>$_POST['cid'],
				'state'=>0,
				'name'=>$name,
			];
			db::update(P_jab."friend")->cols($arr)->where('id='.$_POST['id'])->query();
			$this->Success('修改成功');
		}
		$data=db::select('*')->from(P_jab."friend")->where('id='.$_GET['id']." AND uid=".C('MYDB.uid'))->row();
		if(!$data){
			$this->err('不存在的数据或无权修改');
		}
		$cdata=db::select('*')->from(P_jab."category")->orderByDESC(['reorder'])->query();
		$this->assign('cdata', $cdata);
		$this->assign('data', $data);
		if(DEVICE != 'pc'){
			$this->assign('webname','修改友情链接');
		}
		$this->display(tpl('add'));
	}
	public function del(){
		if(!$_GET['id']){
			$this->err('缺少必要参数');
		}
		$data=db::select('*')->from(P_jab."friend")->where('id='.$_GET['id']." AND uid=".C('MYDB.uid'))->row();
		if(!$data){
			$this->err('不存在的数据或无权删除');
		}
		if($data['thumb']&& !preg_match('/(http|https):\/\/([\w\d\-_]+[\.\w\d\-_]+)[:\d+]?([\/]?[\w\/\.]+)/', $data['thumb'] ) ){
			$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$data['thumb'];
	    	//删除备份文件
			del_article_images($oldthumb);
	    	@unlink($oldthumb);
		}
		db::delete(P_jab."friend")->where('id='.$_GET['id'])->query();
		$this->Success('删除成功');
	}
	
}
?>
